package com.shengyijie.adapter.viewadapter;

import java.util.List;
 
import com.shengyijie.activity.R;
import com.shengyijie.adapter.asyncimageadapter.AsyncImageLoader;
import com.shengyijie.adapter.asyncimageadapter.AsyncImageLoader.ImageCallback; 
import com.shengyijie.adapter.viewcache.MessageListViewCache; 
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.object.baseobject.PushMessage;
import com.shengyijie.util.Utility; 


import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter; 
import android.widget.ImageView;
import android.widget.ListView; 
import android.widget.TextView;


public class MessageListAdapter extends ArrayAdapter<PushMessage>{
 
	private Activity activity;
	private AsyncImageLoader asyncImageLoader;
	private ListView listView;
	
	public MessageListAdapter(Activity activity, List<PushMessage> newsList,ListView listView) {
		  super(activity, 0, newsList);
		  this.activity=activity;  
		  asyncImageLoader = new AsyncImageLoader();
		  this.listView=listView;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		MessageListViewCache viewCache;
		try{ 
				if (convertView == null) {
					LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater(); 
					convertView = inflater.inflate(R.layout.messagelistcell, null);  
					viewCache = new MessageListViewCache(convertView);
					convertView.setTag(viewCache);
				}else{
					viewCache = (MessageListViewCache) convertView.getTag(); 
				}  
				try{ 
					ImageView imageView = viewCache.getImage();  
					String imageUrl=getItem(position).getImageUrl();
					if(imageUrl.equals("")){
						imageView.setVisibility(View.GONE);
					}else{
						imageView.setVisibility(View.VISIBLE);
						imageView.setTag(imageUrl);
						Drawable cachedImage = null;
						
						cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
							public void imageLoaded(Drawable imageDrawable, String imageUrl) {
								ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
								if (imageViewByTag != null) {
									imageViewByTag.setImageDrawable(imageDrawable); 
								}
							}
						});
						
						if (cachedImage == null) {
							imageView.setImageResource(R.drawable.load);
						}else{
							imageView.setImageDrawable(cachedImage);
							cachedImage=null;
						}
					}
					
					
					
					TextView name = viewCache.getTitle();   
					name.setText(getItem(position).getTitle());
					
					TextView des = viewCache.getDes();   
					des.setText(getItem(position).getDes());
					
					TextView time = viewCache.getTime();   
					time.setText(Utility.getTimeGap(getItem(position).getCreatetime()));
					
					TextView type = viewCache.getType();   
					type.setText(ContextApplication.list[getItem(position).getType()]);
					
				}catch(Exception e){
					TextView type = viewCache.getType();   
					type.setText("今日头条");
				}
		}catch(Exception e){}
		return convertView;
	}
}
